package com.hanqian.kepler.web.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hanqian.kepler.common.bean.result.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.Objects;

@Controller
public class RiskController extends BaseController {

    @RequestMapping("risk/{type}")
    public String risk(Model model, @PathVariable String type){
        model.addAttribute("type", type);
        return "risk";
    }

    @RequestMapping("risk/data/{type}")
    @ResponseBody
    public AjaxResult data(@PathVariable String type){
        if(StrUtil.isBlank(type)) return AjaxResult.error("未知类型");
        String path = StrUtil.format("/json/risk_{}.json", type);
        String resourcePath = this.getClass().getResource(path).getPath();
        if(!FileUtil.exist(resourcePath)) return AjaxResult.error("没有此类型的数据【"+type+"】");
        File file = new File(resourcePath);
        JSONObject jsonObject = JSONUtil.readJSONObject(file, CharsetUtil.charset("UTF-8"));
        return AjaxResult.success("获取成功", jsonObject);
    }

}
